package com.crm.web.controller;


import com.crm.entity.Role;
import com.crm.entity.User;
import com.crm.service.IRoleService;
import com.crm.service.IUserService;
import com.crm.shiro.MyShiroRealm;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.mgt.RealmSecurityManager;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/user")
public class UserController {

    @Resource
    private IUserService userService;
    @Resource
    private IRoleService roleService;

    /**
     * 查询
     *
     * @param usrName
     * @param model
     * @param roleId
     * @param pageIndex
     * @return
     */
    @RequestMapping("/list")
    public String findUsers(String usrName, Model model,
                            @RequestParam(defaultValue = "0", required = false) Integer roleId,
                            @RequestParam(required = false, defaultValue = "1") Integer pageIndex) {
        Sort sort = new Sort(Sort.Direction.ASC, "usrId");
        Pageable pageable = PageRequest.of(pageIndex - 1, 5, sort);
        Page<User> userPager = userService.findUsers(usrName, roleId, pageable);
        model.addAttribute("userPager", userPager);
        model.addAttribute("userName", usrName);
        model.addAttribute("roleId", roleId);
        return "/user/list";
    }




    /**
     * 删除
     *
     * @param usrId
     * @return
     */
    @RequestMapping("/del/{usrId}")
    @ResponseBody
    public Map del(@PathVariable Integer usrId) {
        Map<String, String> map = new HashMap<>();
        try {
            userService.deleteUser(usrId);
            map.put("delResult", "true");
        } catch (RuntimeException e) {
            map.put("delResult", "false");
        }
        return map;
    }

    /**
     * 跳转至添加页面
     *
     * @param model
     * @return
     */
    @RequestMapping("/add")
    public String toAdd(Model model) {
        List<Role> roles = roleService.findAllRoles();
        model.addAttribute("roles", roles);
        return "user/add";
    }

    /**
     * 新增和修改
     *
     * @param user
     * @return
     */
    @RequestMapping("/save")
    public String save(User user) {
        userService.saveUser(user);
        RealmSecurityManager rsm=(RealmSecurityManager) SecurityUtils.getSecurityManager();
        MyShiroRealm realm=(MyShiroRealm)rsm.getRealms().iterator().next();
        realm.clearCachedAuthorizationInfo();
        return "redirect:/user/list";
    }

    /**
     * 跳转至修改页面
     *
     * @param usrId
     * @param model
     * @return
     */
//    @RequiresPermissions("用户编辑")
    @RequestMapping("/edit")
    public String edit(@RequestParam Integer usrId, Model model) {
        User user = userService.getUser(usrId);
        List<Role> roles = roleService.findAllRoles();
        model.addAttribute("roles", roles);
        model.addAttribute("user", user);
        return "user/edit";
    }
}
